package websem.database;

import java.util.ArrayList;
import java.util.Map;

public class SPARQLRequest {

	private SPARQLClient sparqlClient;
	private String request;
	
	// 
	
//PREFIX b: <http://ontologies.alwaysdata.net/space#>
//PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
//PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
//
//SELECT ?result ?o ?fils
//WHERE {
//	?o rdfs:label ?label.
//        ?fils rdfs:subClassOf ?o.
//        ?fils rdfs:label ?result.
//        FILTER(regex(?label, "^balade$"))
//}
	
	public SPARQLRequest(String request) throws Exception {
		
		// Connect to server
		this.sparqlClient = new SPARQLClient();
		this.request = request;
		
		if (!sparqlClient.isServerUp()) {
			throw new Exception("SPARQL Server is Down");
		}
	}
	
//	
//	 PUBLIC INTERACTION
//	
	
	/**
	 * Execute
	 */
	public Iterable<Map<String, String>> execute() {
		//TODO:Complete
		// call this.executeSelect()
		// set results 
		// return them (?)
		return this.sparqlClient.select(this.request);
	}
	
	
//	
//	 PRIVATE ENCAPSULATION METHODS
//	
	
	/**
	 * Execute a SPARQL Request 
	 * @param request
	 * @return
	 */
	private Iterable<Map<String, String>> executeSelect() {
		return this.sparqlClient.select(this.request);
	}

	public SPARQLClient getSparqlClient() {
		return sparqlClient;
	}

	public void setSparqlClient(SPARQLClient sparqlClient) {
		this.sparqlClient = sparqlClient;
	}
	/*
	
	public static void main(String args[]) {
        SparqlClient sparqlClient = new SparqlClient("192.168.250.91:8080/sparqluedo");

        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
        if (serverIsUp) {
            System.out.println("server is UP");

            nbPersonnesParPiece(sparqlClient);

            System.out.println("ajout d'une personne dans le bureau:");
            query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "PREFIX instances: <http://www.lamaisondumeurtre.fr/instances#>\n"
                    + "INSERT DATA\n"
                    + "{\n"
                    + "  instances:Bob :personneDansPiece instances:Bureau.\n"
                    + "}\n";
            sparqlClient.update(query);

            nbPersonnesParPiece(sparqlClient);

            System.out.println("suppression d'une personne du bureau:");
            query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "PREFIX instances: <http://www.lamaisondumeurtre.fr/instances#>\n"
                    + "DELETE DATA\n"
                    + "{\n"
                    + "  instances:Bob :personneDansPiece instances:Bureau.\n"
                    + "}\n";
            sparqlClient.update(query);
            
            nbPersonnesParPiece(sparqlClient);
            
        } else {
            System.out.println("service is DOWN");
        }
    }
    
    private static void nbPersonnesParPiece(SparqlClient sparqlClient) {
        String query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "SELECT ?piece (COUNT(?personne) AS ?nbPers) WHERE\n"
                    + "{\n"
                    + "    ?personne :personneDansPiece ?piece.\n"
                    + "}\n"
                    + "GROUP BY ?piece\n";
            Iterable<Map<String, String>> results = sparqlClient.select(query);
            System.out.println("nombre de personnes par pièce:");
            for (Map<String, String> result : results) {
                System.out.println(result.get("piece") + " : " + result.get("nbPers"));
            }
    }    
    */
}
